﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Website_ES
{
    public partial class _Default : System.Web.UI.Page
    {
        DataSet dataBase;
        string sXMLFile = HttpContext.Current.Server.MapPath("~/app_data/Usuarios.xml");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Criar o DataSet
                this.dataBase = new DataSet();

                //Preenche o DataSet com o XML
                this.dataBase.ReadXml(sXMLFile);
            }
        }

        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            //Criando o DataSet
            DataSet ds = new DataSet();
            //Preenche o DataSet com o XML
            ds.ReadXml(this.sXMLFile);

            //Fazer uma busca no DataSet para encontrar o cliente com o esse email
            DataRow []dRow = ds.Tables["Usuarios"].Select(" Email = '" + TextBoxUser.Text + "'");

            if (dRow.Length > 0)
	        {
		        //pega a senha
                string pass = Convert.ToString(dRow[0]["Senha"]);
                string permission = Convert.ToString(dRow[0]["Permission"]);

                if (pass == TextBoxPassword.Text)
                {
                    string url = "homeLogged.aspx?";
                    Session["Usuario"] = TextBoxUser.Text;
                    Session["Permissao"] = permission;
                    Response.Redirect(url);
                }
                else
                    LabelErro.Text = "* Senha incorreta.";
	        }
            else
                LabelErro.Text = "* Usuário inválido.";
        }
    }
}
